package PlayerManager;

import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.Random;

import Cards.Card;
import Cards.MailCardBill;
import Cards.MailCardMakeDeal;
import GameFiles.StateBase;
import Gui.GameFrame;
import PlayerStates.InitialLoad;

public class Player {
	private int m_ID;
	private int m_Cash;
	private String m_Name;
	private int m_MapX;
	private int m_MapY;
	private int m_Diameter;
	private int m_Day;
	private int m_Month;
	private int m_loanAmount;
	private boolean m_Done;
	private LinkedList<Card> DealCards;
	private LinkedList<Card> MailCards;
	private LottoCard m_lottoCard;
	// State Managers
	private StateBase PreviousState;
	private StateBase CurrentState;

	// Graphics stuff
	Color m_Color;

	public Player() {
		Random rand = new Random();
		m_Color = new Color(rand.nextInt(256), rand.nextInt(256), rand
				.nextInt(256));
		CurrentState = InitialLoad.getInstance();
		PreviousState = InitialLoad.getInstance();
		DealCards = new LinkedList<Card>();
		MailCards = new LinkedList<Card>();
		m_lottoCard = new LottoCard(); // constructor gets chances from chance
										// list
		setM_Radius(50);
		m_Month = 1;
	}

	public void addMailCard(MailCardMakeDeal mailCard) {
		MailCards.add(mailCard);
	}

	public void addMailCard(MailCardBill mailCard) {
		MailCards.add(mailCard);
	}

	public void addDealCard(Card dealCard) {
		DealCards.add(dealCard);
	}

	// TODO WARNING PASS BY VALUE NOT REFERENCE
	public LinkedList<Card> getDealCards() {
		return DealCards;
	}

	public LinkedList<Card> getMailCards() {
		return MailCards;
	}

	// Id
	public void setID(int a_ID) {
		Random rand = new Random();
		m_ID = a_ID;
		m_Color = new Color(rand.nextInt(256), rand.nextInt(256), rand
				.nextInt(256));
	}

	public int getID() {
		return m_ID;
	}

	public int getIDPrintable() {
		return m_ID + 1;
	}

	// Name
	public void setName(String a_name) {
		m_Name = a_name;
	}

	public String getName() {
		return m_Name;
	}

	// Cash
	public void setCash(int m_Cash) {
		this.m_Cash = m_Cash;
	}

	public void AddCash(int m_Cash) {
		this.m_Cash += m_Cash;
	}

	public void RemoveCash(int m_Cash) {
		this.m_Cash -= m_Cash;
	}

	public int getCash() {
		return m_Cash;
	}

	// loan amount
	public int getLoan() {
		return m_loanAmount;
	}

	public void AddLoan(int value) {
		m_loanAmount += value;
	}

	public void setLoan(int value) {
		m_loanAmount = value;
	}

	// lotto stuff
	public LottoCard getLottoCard() {
		return m_lottoCard;
	}

	// Gamemap functions
	public void setMapX(int a_MapX) {
		m_MapX = a_MapX;
	}

	public void setMapY(int a_MapY) {
		m_MapY = a_MapY;
	}

	public void setDay(int a_Day) {
		m_Day = a_Day;
	}

	public int getMapX() {
		return m_MapX;
	}

	public int getMapY() {
		return m_MapY;
	}

	public int getDay() {
		return m_Day;
	}

	public String getGameOverToString() {
		String s = String
				.format(
						"Player $%d<br> Cash: $%d<br> Loan: $%d<br> Final Balance: $%d<br>",
						getIDPrintable(), getCash(), getLoan(), getCash()
								- getLoan());
		return s;
	}

	// Gameloop processes
	// TODO : I removed 'static' bms.
	public void Update() {
		CurrentState.Execute();
	}

	// TODO : I removed 'static' bms.
	// TODO : review DrawPlayer.
	public void DrawPlayer(Graphics g, int height, int width, int rows, int cols) {
		int row = m_Day / cols;
		int col = m_Day % cols;
		m_MapX = col * width / cols + width / (2 * cols);
		m_MapY = row * height / rows + height / (2 * rows);
		if (width / cols > height / rows) {
			m_Diameter = height / (rows * 2);
		}
		else {
			m_Diameter = width / (cols * 2);
		}

		Color newcolor = null;
		if (m_Color == null) {
			newcolor = Color.BLACK;
		}
		else {
			newcolor = m_Color;
		}
		Color oldcolor = g.getColor();
		g.setColor(newcolor);
		g.fillOval(m_MapX - m_Diameter / 2, m_MapY - m_Diameter / 2,
				m_Diameter, m_Diameter);
		g.setColor(oldcolor);
	}

	// TODO : I removed 'static' bms.
	public void Draw(GameFrame gf) {
		CurrentState.Draw(gf);
	}

	public void setDone(boolean m_Done) {
		this.m_Done = m_Done;
	}

	public boolean getDone() {
		return m_Done;
	}

	public void setPreviousState(StateBase previousState) {
		PreviousState = previousState;
	}

	public StateBase getPreviousState() {
		return PreviousState;
	}

	public void setCurrentState(StateBase currentState) {
		CurrentState = currentState;
	}

	public StateBase getCurrentState() {
		return CurrentState;
	}

	public void setM_Radius(int m_Radius) {
		this.m_Diameter = m_Radius;
	}

	public int getM_Radius() {
		return m_Diameter;
	}

	public void setMonth(int m_Month) {
		this.m_Month = m_Month;
	}

	public int getMonth() {
		return m_Month;
	}
}
